<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="数据结构," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="数据结构与算法数据结构与算法是计算机学科中的重要基础，而且对数据结构与算法有比较深刻的了解，对我们以后从事计算机相关工作有很多的帮助。同时，很多人都认为数据结构与算法的学习是一种思维的锻炼（内功的修炼）。当开始在学校学习这门课的时候自己没有意识到这门课的意义，当领悟到数据结构与算法的重要性的时候，只能自己去回顾一遍这门课了，加深自己的修为……
数据的逻辑结构
线性结构：除了头尾，每个数据元素都只有">
<meta property="og:type" content="article">
<meta property="og:title" content="数据结构--线性表">
<meta property="og:url" content="https://zitongchen.github.io/2017/03/15/数据结构--线性表/index.html">
<meta property="og:site_name" content="Tony's Notes">
<meta property="og:description" content="数据结构与算法数据结构与算法是计算机学科中的重要基础，而且对数据结构与算法有比较深刻的了解，对我们以后从事计算机相关工作有很多的帮助。同时，很多人都认为数据结构与算法的学习是一种思维的锻炼（内功的修炼）。当开始在学校学习这门课的时候自己没有意识到这门课的意义，当领悟到数据结构与算法的重要性的时候，只能自己去回顾一遍这门课了，加深自己的修为……
数据的逻辑结构
线性结构：除了头尾，每个数据元素都只有">
<meta property="og:updated_time" content="2017-03-15T08:47:06.882Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="数据结构--线性表">
<meta name="twitter:description" content="数据结构与算法数据结构与算法是计算机学科中的重要基础，而且对数据结构与算法有比较深刻的了解，对我们以后从事计算机相关工作有很多的帮助。同时，很多人都认为数据结构与算法的学习是一种思维的锻炼（内功的修炼）。当开始在学校学习这门课的时候自己没有意识到这门课的意义，当领悟到数据结构与算法的重要性的时候，只能自己去回顾一遍这门课了，加深自己的修为……
数据的逻辑结构
线性结构：除了头尾，每个数据元素都只有">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://zitongchen.github.io/2017/03/15/数据结构--线性表/"/>





  <title> 数据结构--线性表 | Tony's Notes </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Tony's Notes</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://zitongchen.github.io/2017/03/15/数据结构--线性表/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tony">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avater.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Tony's Notes">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                数据结构--线性表
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-03-15T00:00:00+08:00">
                2017-03-15
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/数据结构/" itemprop="url" rel="index">
                    <span itemprop="name">数据结构</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/03/15/数据结构--线性表/" class="leancloud_visitors" data-flag-title="数据结构--线性表">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="数据结构与算法"><a href="#数据结构与算法" class="headerlink" title="数据结构与算法"></a>数据结构与算法</h2><p>数据结构与算法是计算机学科中的重要基础，而且对数据结构与算法有比较深刻的了解，对我们以后从事计算机相关工作有很多的帮助。同时，很多人都认为数据结构与算法的学习是一种思维的锻炼（内功的修炼）。当开始在学校学习这门课的时候自己没有意识到这门课的意义，当领悟到数据结构与算法的重要性的时候，只能自己去回顾一遍这门课了，加深自己的修为……</p>
<h3 id="数据的逻辑结构"><a href="#数据的逻辑结构" class="headerlink" title="数据的逻辑结构"></a>数据的逻辑结构</h3><ul>
<li>线性结构：除了头尾，每个数据元素都只有唯一的前驱节点和后驱节点</li>
<li>树型结构：除根节点外，每个数据元素有零个或多个前驱数据元素和后继数据元素</li>
<li>图型结构：每个数据元素都有零个或多个前驱数据元素和后继数据元素</li>
</ul>
<h3 id="数据的存储结构"><a href="#数据的存储结构" class="headerlink" title="数据的存储结构"></a>数据的存储结构</h3><ul>
<li>顺序存储结构：把数据元素存储到一块连续的存储地址的空间的内存中</li>
<li>链式存储结构：使用指针把相互直接关联的结点连接起来，逻辑相邻的数据元素在物理上不一定相邻</li>
</ul>
<h3 id="算法的性质和设计目标"><a href="#算法的性质和设计目标" class="headerlink" title="算法的性质和设计目标"></a>算法的性质和设计目标</h3><h4 id="算法应满足的性质"><a href="#算法应满足的性质" class="headerlink" title="算法应满足的性质"></a>算法应满足的性质</h4><ul>
<li>输入性：具有零个或若干个输入量</li>
<li>输出性：至少产生一个输出量或执行一个有意义的操作</li>
<li>有限性：执行语句的序列是有限的</li>
<li>确定性：每条语句的含义明确，无二义性</li>
<li>可执行性：每条语句都应该有限的时间内完成</li>
</ul>
<h4 id="算法设计应满足的目标"><a href="#算法设计应满足的目标" class="headerlink" title="算法设计应满足的目标"></a>算法设计应满足的目标</h4><ul>
<li>正确性：满足具体问题的需求</li>
<li>可读性：便于人们对算法的理解</li>
<li>健壮性：当输入非法数据时，算法能做出相应的处理，不应产生不可预料的后果</li>
<li>高时间效率：算法运行的时间要足有短</li>
<li>高空间效率：算法运行时所占用的空间要小</li>
</ul>
<h4 id="算法的时间效率"><a href="#算法的时间效率" class="headerlink" title="算法的时间效率"></a>算法的时间效率</h4><p>用时间复杂度来分析一个算法的时间效率，通常使用O(f(n))表示法。算法的时间复杂度是衡量一个算法好坏的重要指标，一般来说多项式时间复杂度(如O(n),O( n^2),O(n^8))的算法是可以接受的，可用的，但指数时间复杂度(如O(2^n),O(n!))的算法理论上是可计算的，但实际上是无法接受的</p>
<h2 id="线性表"><a href="#线性表" class="headerlink" title="线性表"></a>线性表</h2><h3 id="顺序表"><a href="#顺序表" class="headerlink" title="顺序表"></a>顺序表</h3><p>C语言实现顺序表的时候是才用数组的方式的把数据元素储存到一块连续的地址空间里面存储，把定义顺序表操作的实现保存到<code>SeqList.h</code>头文件。</p>
<figure class="highlight c"><table><tr><td class="code"><pre><div class="line"><span class="comment">//SeqList.h</span></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span></div><div class="line">&#123;</div><div class="line">    DataType <span class="built_in">list</span>[MaxSize]; <span class="comment">//顺序表最大可存储的元素个数</span></div><div class="line">    <span class="keyword">int</span> size;</div><div class="line">&#125; SeqList;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ListInitiate</span><span class="params">(SeqList *L)</span></span></div><div class="line">&#123;</div><div class="line">    L-&gt;size = <span class="number">0</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListLength</span><span class="params">(SeqList L)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">return</span> L.size;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//插入数据</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListInsert</span><span class="params">(SeqList *L, <span class="keyword">int</span> i, DataType x)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> j;</div><div class="line">    <span class="keyword">if</span>(L-&gt;size &gt;= MaxSize)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"顺序表已满，无法插入数据\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(i &lt; <span class="number">0</span> || i &gt; L-&gt;size)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"参数i不合法\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span> ;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">for</span>(j = L-&gt;size; j &gt; i; j--)</div><div class="line">        &#123;</div><div class="line">            L-&gt;<span class="built_in">list</span>[j] = L-&gt;<span class="built_in">list</span>[j - <span class="number">1</span>];</div><div class="line">        &#125;</div><div class="line">        L-&gt;<span class="built_in">list</span>[i] = x;</div><div class="line">        L-&gt;size ++;</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListDelete</span><span class="params">(SeqList *L, <span class="keyword">int</span> i, DataType *x)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> j ;</div><div class="line">    <span class="keyword">if</span>(L-&gt;size &lt;= <span class="number">0</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"顺序表已经没有元素\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span> <span class="keyword">if</span>(L-&gt;size &lt; i)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"参数i不合法\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span></div><div class="line">    &#123;</div><div class="line">        *x = L-&gt;<span class="built_in">list</span>[i];</div><div class="line">        <span class="keyword">for</span>(j = i; j &lt;= L-&gt;size - <span class="number">1</span>; j++)</div><div class="line">        &#123;</div><div class="line">            L-&gt;<span class="built_in">list</span>[j] = L-&gt;<span class="built_in">list</span>[j + <span class="number">1</span>];</div><div class="line">        &#125;</div><div class="line">        L-&gt;size--;</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//去SeqList里面的元素</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListGet</span><span class="params">(SeqList L, <span class="keyword">int</span> i, DataType *x)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">if</span>(i &lt; <span class="number">0</span> || i &gt; MaxSize)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"参数i不合法"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span></div><div class="line">    &#123;</div><div class="line">        *x = L.<span class="built_in">list</span>[i];</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//删除指定的元素</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListDataDelete</span><span class="params">(SeqList *L, DataType x)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> i, j;</div><div class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; L-&gt;size; i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(x == L-&gt;<span class="built_in">list</span>[i]) <span class="keyword">break</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span>(i == L-&gt;size)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"顺序链表中不存在%d\n"</span>, x);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span></div><div class="line">    &#123;</div><div class="line">        <span class="keyword">for</span>(j = i; j &lt; L-&gt;size - <span class="number">1</span>; j++)</div><div class="line">        &#123;</div><div class="line">            L-&gt;<span class="built_in">list</span>[j] = L-&gt;<span class="built_in">list</span>[j + <span class="number">1</span>];</div><div class="line">        &#125;</div><div class="line">        L-&gt;size--;</div><div class="line">        <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//删除指定值的所以元素</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListDataDeleteAll</span><span class="params">(SeqList *L, DataType x)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> i = <span class="number">0</span>, j, tag;</div><div class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; L-&gt;size; i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span>(x == L-&gt;<span class="built_in">list</span>[i])</div><div class="line">        &#123;</div><div class="line">            <span class="keyword">for</span>(j = i; j &lt; L-&gt;size; j++)</div><div class="line">            &#123;</div><div class="line">                L-&gt;<span class="built_in">list</span>[j] = L-&gt;<span class="built_in">list</span>[j + <span class="number">1</span>]</div><div class="line">            &#125;</div><div class="line">            L-&gt;size--</div><div class="line">            tag = <span class="number">1</span>;</div><div class="line">            i--;<span class="comment">//返回一个位置，保证两个相邻的元素均能够删除</span></div><div class="line">        &#125;</div><div class="line"></div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> tag;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//排序(冒泡排序)循环n-1次，比较也是n-1次</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ListSort</span><span class="params">(SeqList *L)</span></span></div><div class="line">&#123;</div><div class="line">    <span class="keyword">int</span> swap;</div><div class="line">    <span class="keyword">int</span> i = <span class="number">0</span>;</div><div class="line">    <span class="keyword">int</span> j = <span class="number">0</span>;</div><div class="line">    <span class="keyword">for</span>(i = <span class="number">1</span>; i &lt; L-&gt;size; i++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">for</span>(j = <span class="number">0</span>; j &lt; L-&gt;size - i; j++) <span class="comment">//比较的顺序是有前往后比较的</span></div><div class="line">        &#123;</div><div class="line">            <span class="keyword">if</span>(L-&gt;<span class="built_in">list</span>[j] &gt; L-&gt;<span class="built_in">list</span>[j + <span class="number">1</span>])</div><div class="line">            &#123;</div><div class="line">                swap = L-&gt;<span class="built_in">list</span>[j];</div><div class="line">                L-&gt;<span class="built_in">list</span>[j] = L-&gt;<span class="built_in">list</span>[j + <span class="number">1</span>];</div><div class="line">                L-&gt;<span class="built_in">list</span>[j + <span class="number">1</span>] = swap;</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>顺序表的应用举例<code>SeqListMain.c</code></p>
<figure class="highlight c"><table><tr><td class="code"><pre><div class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></div><div class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">&lt;math.h&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MaxSize 50 <span class="comment">//定义MaxSize为50</span></span></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">int</span> DataType; <span class="comment">//定义DataType为int</span></div><div class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">"SeqList.h"</span>  <span class="comment">//包含SeqList.h头文件，因为在头文件中要应用MaxSize，DataType所以，两者要在引入头文件前定义好</span></span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line">&#123;</div><div class="line">    SeqList myList;</div><div class="line">    <span class="keyword">int</span> i, x;</div><div class="line">    ListInitiate(&amp;myList);</div><div class="line"></div><div class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++)</div><div class="line">    &#123;</div><div class="line">        ListInsert(&amp;myList, i, rand());</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//排序前的顺序表</span></div><div class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; myList.size; i++)</div><div class="line">    &#123;</div><div class="line">        ListGet(myList, i, &amp;x);</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d   "</span>, x);</div><div class="line">    &#125;</div><div class="line">    <span class="comment">//给顺序表排序</span></div><div class="line">    ListSort(&amp;myList);</div><div class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);</div><div class="line">    <span class="comment">//排序后的顺序表</span></div><div class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; myList.size; i++)</div><div class="line">    &#123;</div><div class="line">        ListGet(myList, i, &amp;x);</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d   "</span>, x);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>链表的实现保存到<code>LinList.h</code>中，链表中数据元素间物理地址不一定相邻，数据元素间的逻辑关系表现在结点的链接关系上</p>
<figure class="highlight c"><table><tr><td class="code"><pre><div class="line"><span class="comment">//LinList.h</span></div><div class="line"><span class="comment">//链表的结构体</span></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> Node</div><div class="line">&#123;</div><div class="line">    DataType data;</div><div class="line">    <span class="keyword">struct</span> Node *next;</div><div class="line">&#125; SLNode;</div><div class="line"></div><div class="line"><span class="comment">//head设置成指向指针的指针才可以返回参数head的值</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">ListInitiate</span><span class="params">(SLNode **head)</span></span></div><div class="line">&#123;</div><div class="line">    *head = (SLNode *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(SLNode));</div><div class="line">    (*head)-&gt;next = <span class="literal">NULL</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListLength</span><span class="params">(SLNode *head)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *p = head;</div><div class="line">    <span class="keyword">int</span> size = <span class="number">0</span>;</div><div class="line">    <span class="keyword">while</span>(p-&gt;next != <span class="literal">NULL</span>)</div><div class="line">    &#123;</div><div class="line">        p = p-&gt;next ;</div><div class="line">        size ++;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> size;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListInsert</span><span class="params">(SLNode *head, <span class="keyword">int</span> i, DataType x)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *p , *q;</div><div class="line">    <span class="keyword">int</span> j;</div><div class="line">    p = head;</div><div class="line">    j = <span class="number">-1</span>;</div><div class="line">    <span class="keyword">while</span>(p-&gt;next != <span class="literal">NULL</span> &amp;&amp; j &lt; i - <span class="number">1</span>) <span class="comment">//循环计数</span></div><div class="line">    &#123;</div><div class="line">        p = p-&gt;next;</div><div class="line">        j++;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span>(j != i - <span class="number">1</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"插入的位置参数出错\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    q = (SLNode *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(SLNode));</div><div class="line">    q-&gt;data = x;</div><div class="line">    q-&gt;next = p-&gt;next;</div><div class="line">    p-&gt;next = q;</div><div class="line">    <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListDelete</span><span class="params">(SLNode *head , <span class="keyword">int</span> i , DataType *x)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *p, *q;</div><div class="line">    <span class="keyword">int</span> j;</div><div class="line">    p = head;</div><div class="line">    j = <span class="number">-1</span>;</div><div class="line">    <span class="comment">//通过循环到达了要删除位置的前一个位置 </span></div><div class="line">    <span class="keyword">while</span>(p-&gt;next != <span class="literal">NULL</span> &amp;&amp; p-&gt;next -&gt;next != <span class="literal">NULL</span> &amp;&amp; j &lt; i - <span class="number">1</span>) </div><div class="line">    &#123;</div><div class="line">        p = p-&gt;next;</div><div class="line">        j++;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span>(j != i - <span class="number">1</span>)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"位置参数i出错\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    q = p -&gt; next;</div><div class="line">    *x = q-&gt;data;</div><div class="line">    p -&gt;next = p -&gt;next -&gt;next;</div><div class="line">    <span class="built_in">free</span>(q);</div><div class="line">    <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">ListGet</span><span class="params">(SLNode *head, <span class="keyword">int</span> i, DataType *x)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *p;</div><div class="line">    <span class="keyword">int</span> j;</div><div class="line">    p = head;</div><div class="line">    j = <span class="number">-1</span>;</div><div class="line">    <span class="keyword">while</span>(p-&gt;next != <span class="literal">NULL</span> &amp;&amp; j &lt; i)</div><div class="line">    &#123;</div><div class="line">        p = p-&gt;next;</div><div class="line">        j++;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span>(j != i)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"位置参数i出错\n"</span>);</div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    *x = p-&gt;data;</div><div class="line">    <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Destroy</span><span class="params">(SLNode **head)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *p , *p1;</div><div class="line">    p = *head;</div><div class="line">    <span class="keyword">while</span>(p != <span class="literal">NULL</span>)</div><div class="line">    &#123;</div><div class="line">        p1 = p;</div><div class="line">        p = p-&gt;next;</div><div class="line">        <span class="built_in">free</span>(p1);</div><div class="line">    &#125;</div><div class="line">    *head = <span class="literal">NULL</span>;</div><div class="line">&#125;</div><div class="line"><span class="comment">//按元素大小插入到合适的位置</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">LinListInsert</span><span class="params">(SLNode *head, DataType x)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *curr , *pre, *q;</div><div class="line">  	curr = head -&gt;next;</div><div class="line">    pre = head;</div><div class="line">    <span class="comment">//定位到插入的位置，退出循环是，pre指针指向要插入的节点的位置</span></div><div class="line">    <span class="keyword">while</span>(curr != <span class="literal">NULL</span> &amp;&amp; curr-&gt;data &lt; x)</div><div class="line">    &#123;</div><div class="line">        pre = curr;</div><div class="line">        curr = curr-&gt;next;</div><div class="line">    &#125;</div><div class="line">    q = (SLNode *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(SLNode));</div><div class="line">    q-&gt;data = x;</div><div class="line">    q-&gt;next = pre-&gt;next;</div><div class="line">    pre-&gt;next = q;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//链表元素的排序，实现不增加新的内存空间的设计要求</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">LinListSort</span><span class="params">(SLNode *head)</span></span></div><div class="line">&#123;</div><div class="line">    SLNode *curr, *pre, *p, *q;</div><div class="line">    p = head-&gt;next;  <span class="comment">//p指针所指中的节点要一个一个取出作为插入节点，就变成上述排序插入一样了</span></div><div class="line">    head-&gt;next = <span class="literal">NULL</span>;  <span class="comment">//head指针带头指针，初始为空链</span></div><div class="line">    <span class="keyword">while</span>(p != <span class="literal">NULL</span>)</div><div class="line">    &#123;</div><div class="line">        curr = head-&gt;next;</div><div class="line">        pre = head;</div><div class="line">        <span class="comment">//定位到插入的位置，退出循环是，pre指针指向要插入的节点的位置</span></div><div class="line">        <span class="keyword">while</span>(curr != <span class="literal">NULL</span> &amp;&amp; curr-&gt;data &lt;= p-&gt;data)</div><div class="line">        &#123;</div><div class="line">            pre = curr;</div><div class="line">            curr = curr-&gt;next;</div><div class="line">        &#125;</div><div class="line">        q = p; <span class="comment">//q指针指向要插入的节点</span></div><div class="line">        p = p-&gt;next; <span class="comment">//p指针指向下一个节点</span></div><div class="line">        <span class="comment">//把q指针所指的节点插入到pre指针所指的节点后面</span></div><div class="line">        q-&gt;next = pre-&gt;next; </div><div class="line">        pre-&gt;next = q;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>链表的应用举例<code>LinListMain.c</code></p>
<figure class="highlight c"><table><tr><td class="code"><pre><div class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></div><div class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">&lt;malloc.h&gt;</span></span></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">int</span> DataType;</div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"LinList.h"</span></span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span>  <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></div><div class="line">&#123;</div><div class="line">	SLNode *head;</div><div class="line">	<span class="keyword">int</span> i,x;</div><div class="line">	ListInitiate(&amp;head);</div><div class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">10</span>;i++)</div><div class="line">	&#123;</div><div class="line">		ListInsert(head,i,i+<span class="number">1</span>);</div><div class="line">	&#125;</div><div class="line">	ListDelete(head,<span class="number">4</span>,&amp;x);</div><div class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;ListLength(head);i++)</div><div class="line">	&#123;</div><div class="line">		ListGet(head,i,&amp;x);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"%d   "</span>,x);</div><div class="line">	&#125;</div><div class="line">	Destroy(&amp;head);</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">//结果：1 2 3 4 6 7 8 9 10</span></div></pre></td></tr></table></figure>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>线性表是一种最简单的线性结构。线性表主要的操作特点是<strong>可以在任意位置插入和删除一个数据元素</strong>。线性表可以用顺序存储结构和链式存储结构存储，分别称为顺序表和链表。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/数据结构/" rel="tag"># 数据结构</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/15/数据结构--队列/" rel="next" title="数据结构--队列">
                <i class="fa fa-chevron-left"></i> 数据结构--队列
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/03/15/数据结构--堆栈/" rel="prev" title="数据结构--堆栈">
                数据结构--堆栈 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="uyan_frame"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avater.png"
               alt="Tony" />
          <p class="site-author-name" itemprop="name">Tony</p>
           
              <p class="site-description motion-element" itemprop="description">学习技术，分享技术...</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">25</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/zitongchen" rel="external nofollow" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#数据结构与算法"><span class="nav-number">1.</span> <span class="nav-text">数据结构与算法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据的逻辑结构"><span class="nav-number">1.1.</span> <span class="nav-text">数据的逻辑结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据的存储结构"><span class="nav-number">1.2.</span> <span class="nav-text">数据的存储结构</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#算法的性质和设计目标"><span class="nav-number">1.3.</span> <span class="nav-text">算法的性质和设计目标</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#算法应满足的性质"><span class="nav-number">1.3.1.</span> <span class="nav-text">算法应满足的性质</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#算法设计应满足的目标"><span class="nav-number">1.3.2.</span> <span class="nav-text">算法设计应满足的目标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#算法的时间效率"><span class="nav-number">1.3.3.</span> <span class="nav-text">算法的时间效率</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#线性表"><span class="nav-number">2.</span> <span class="nav-text">线性表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#顺序表"><span class="nav-number">2.1.</span> <span class="nav-text">顺序表</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number"></span> <span class="nav-text">总结</span></a></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io" rel="external nofollow">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  
    

    
      <!-- UY BEGIN -->
      <script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2130825"></script>
      <!-- UY END -->
    
  





  



  
  

  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("4wyUmphSKE5wrQGHEh5Js0bk-gzGzoHsz", "oTOHrhMuED5U0oqV2KrRMGF0");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  


  
</body>
</html>
